var NEWS = require('../../../class/news/news.c');

module.exports = {
    prefix: '/user/news/',
    get: {
        'list': {
            des: {
                name: 'getNewsList',
                content: '获取资讯列表',
                params: [
                    {
                        query: 'page',
                        type: 'Number,页码'
                    },
                    {
                        query: 'num',
                        type: 'Number,单页数量'
                    }
                ]
            },
            method: function* () {
                var self = this;
                try {
                    var query = self.request.query;
                    var _list = yield NEWS.list(query);
                    self.body = _list;
                } catch (e) {
                    self.body = printError(e);
                }
            }
        },
        'detail/:_id': {
            des: {
                name: 'getNewsDetail',
                content: '获取资讯详情',
                params: [
                    {
                        params: '_id',
                        type: 'String,资讯_id，在链接上直接拼接'
                    }
                ]
            },
            method: function* () {
                var self = this;
                try {
                    var _id = self.params._id;
                    var _detail = yield NEWS.detail(_id);
                    var readable = self.session.newsreadable || {};
                    !self.session.newsreadable ? self.session.newsreadable = {} : false;
                    var _canupdate = false;
                    (_id in readable) ? (+new Date) - readable[_id] > 1000 * 60 * 30 ?
                        _canupdate = true : false :
                        _canupdate = true;
                    self.body = _detail;

                    if (_canupdate && _detail) {
                        var ov = Object.keys(readable);
                        ov.length > 9 ? delete self.session.newsreadable[ov[0]] : false;
                        self.session.newsreadable[_id] = +new Date;
                        yield NEWS.update({ _id: _id, viewtotal: parseInt(_detail.viewtotal) || 0 });
                    }

                } catch (e) {
                    self.body = printError(e);
                }
            }
        },
        'count': {
            des: {
                name: 'getNewsCount',
                content: '获取资讯统计',
            },
            method: function* () {
                var self = this;
                var _count = yield NEWS.count();
                self.body = _count || 0;
            }
        }
    }
}